package com.sbpm.crmbpm.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import com.sbpm.crmbpm.client.deprecforms.OnFinishCallBack;
import com.sbpm.crmbpm.client.deprecforms.controls.UsersComboBox;
import com.sbpm.crmbpm.client.resources.images.ImageResources;
import com.sbpm.crmbpm.client.grid.TaskGrid;
import com.sbpm.crmbpm.client.services.ProcessService;
import com.sbpm.crmbpm.client.services.ProcessServiceAsync;
import com.sbpm.crmbpm.client.util.TaskProcessor;
import com.sbpm.crmbpm.shared.data.TaskInfo;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.*;
import com.sencha.gxt.widget.core.client.button.ButtonBar;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @deprecated
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Insurance implements EntryPoint {

    /**
     * Create a remote service proxy to talk to the server-side Greeting service.
     */
    private final ProcessServiceAsync processService = GWT.create(ProcessService.class);


    private ButtonBar buttonBar;

    private FramedPanel panel;

    private com.sencha.gxt.widget.core.client.grid.Grid grid;
    private UsersComboBox usersComboBox;
    private TaskProcessor taskProcessor;

    public Widget createPanel() {

        if( panel == null )
        {
            panel = new FramedPanel();
            panel.setHeadingText("jBPM");
            panel.setWidth( "100%" );
            panel.setLayoutData(new MarginData(10));

        }

        HorizontalLayoutContainer horizontalLayoutContainer = new HorizontalLayoutContainer();


        usersComboBox = new UsersComboBox();
        usersComboBox.addChangeHandler( new ChangeHandler()
        {
            @Override
            public void onChange(ChangeEvent event)
            {
                refreshTaskGrid( usersComboBox.getItemText( usersComboBox.getSelectedIndex() ) );
            }
        } );

        horizontalLayoutContainer.add( usersComboBox );

        if (buttonBar == null)
        {
            buttonBar = new ButtonBar();
            buttonBar.setMinButtonWidth(75);
            buttonBar.getElement().setMargins(10);
//            buttonBar.setHeight( 40 );

            TextButton b = new TextButton("Создать заявку");
            b.setIcon( ImageResources.INSTANCE.addIcon() );
            b.addSelectHandler(new SelectHandler() {
                public void onSelect(SelectEvent event) {

                    processService.createInsuranceRequestProcessInstance( new AsyncCallback< Long >()
                    {
                        @Override
                        public void onFailure( Throwable caught )
                        {
                            AlertMessageBox d = new AlertMessageBox( "Error", "Result - " + caught.getMessage() );
                            d.show();
                        }

                        @Override
                        public void onSuccess( Long result )
                        {
                            MessageBox d = new MessageBox( "Success", "Result - " + result );
                            d.show();
                            refreshTaskGrid();
                            //taskProcessor.startTask( result, TaskProcessor.STATUS_READY, "Operator1" );

                        }
                    } );
                }
            });
            buttonBar.add(b);

            TextButton processTaskButton = new TextButton( "Обработать задачу" );
            processTaskButton.addSelectHandler(new SelectHandler() {
                @Override
                public void onSelect(SelectEvent event)
                {
                    taskProcessor.startTask();
                }
            });
            buttonBar.add( processTaskButton );
        }



        horizontalLayoutContainer.add( buttonBar );
        horizontalLayoutContainer.setStyleName( Style.VerticalAlign.MIDDLE.getCssName() );

        VerticalLayoutContainer verticalLayoutContainer = new VerticalLayoutContainer();

        verticalLayoutContainer.add( horizontalLayoutContainer, new VerticalLayoutContainer.VerticalLayoutData(1, 50, new Margins(4)) );

        TaskGrid taskGrid = new TaskGrid();

        grid = taskGrid.createTaskGrid( new ArrayList() );
        grid.setHeight( "300px" );
//        grid.setPixelSize(200, 200);
        grid.setWidth( "100%" );

        verticalLayoutContainer.add(grid, new VerticalLayoutContainer.VerticalLayoutData(1., 300, new Margins(4)));

        panel.setWidget( verticalLayoutContainer );

        taskProcessor = new TaskProcessor( usersComboBox, grid, new OnFinishCallBack()
        {
            @Override
            public void onFinish()
            {
                refreshTaskGrid();
            }
        } );

        return panel;
    }

    private void refreshTaskGrid()
    {
        refreshTaskGrid( usersComboBox.getItemText( usersComboBox.getSelectedIndex() ) );
    }

    private void refreshTaskGrid( String user )
    {
        processService.getTaskList(user, new AsyncCallback<List<TaskInfo>>() {
            @Override
            public void onFailure(Throwable caught) {
            }

            @Override
            public void onSuccess(List<TaskInfo> result) {
                grid.getStore().clear();
                grid.getStore().addAll( result );

                //grid.reconfigure( grid.getStore(), grid.getColumnModel() );
                grid.getView().refresh( false );
//                MessageBox box = new MessageBox( "store size = " + grid.getStore().size() + ", result size = " + result.size() );
//                box.show();
            }
        });
    }

    public void onModuleLoad() {
        RootPanel.get().add(createPanel());


       /* TaskGrid taskGrid = new TaskGrid();
        com.sencha.gxt.widget.core.client.grid.Grid grid = taskGrid.createTaskGrid();
		RootPanel.get().add(grid);*/

    }


}
